﻿using SqlSugar;
using StoreSQLSugar_API.Models;
using StoreSQLSugar_API.Services;
using StoreSQLSugar_API.Services.ServiceImpls;

namespace TodoAPI.Services.ServiceImpls
{
    public class UserService : IUserService
    {
        private ISqlSugarClient _db;
        // 把ISqlSugarClient服务注入到UserService中。
        public UserService(ISqlSugarClient db)
        {
            _db = db;
        }

        public bool Add(User model)
        {
            return _db.Insertable<User>(model).ExecuteCommand() > 0;
        }

        public bool Delete(int primaryKey)
        {
            return _db.Deleteable<User>(primaryKey).ExecuteCommand() > 0;
        }

        public List<User> GetList(Expressionable<User> exp, out int totalPage, int pageIndex = 1, int pageSize = 10)
        {
            totalPage = 0;
            int totalNumber = 0;
            return _db.Queryable<User>().Where(exp.ToExpression()).ToPageList(pageIndex, pageSize, ref totalNumber, ref totalPage);
        }

        public List<User> GetListAll(Expressionable<User> exp)
        {
            return _db.Queryable<User>().Where(exp.ToExpression()).ToList();
        }

        public User GetModel(int primaryKey)
        {
            return _db.Queryable<User>().InSingle(primaryKey);
        }

        public bool Update(User model)
        {
            return _db.Updateable<User>(model).ExecuteCommand() > 0;
        }
    }
}
